import router from './router/'
import store from './store'
import {validatenull} from '@/utils/validate'
import {getToken} from '@/utils/auth'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({showSpinner: false});
const lockPage = '/lock'; //锁屏页
router.beforeEach((to, from, next) => {
    const meta = to.meta || {};
    const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
    store.commit('SET_IS_MENU', isMenu === undefined);
    if (getToken()) {
        if (store.getters.isLock && to.path != lockPage) { //如果系统激活锁屏，全部跳转到锁屏页
            next({path: lockPage})
        } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
            next({path: '/'})
        } else {
            //如果用户信息为空则获取用户信息，获取用户信息失败，跳转到登录页
            // if (store.getters.roles.length === 0) {
            //     store.dispatch('GetUserInfo',getToken()).then(() => {
            //         next({...to, replace: true})
            //     }).catch(() => {
            //         store.dispatch('FedLogOut').then(() => {
            //             next({path: '/login'})
            //         })
            //     })
            // } else {
            let value = to.query.src || to.fullPath
            const label = to.query.name || to.name
            const meta = to.meta || router.$avueRouter.meta || {}
            const i18n = to.query.i18n
            if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
                store.commit('ADD_TAG', {
                    label: label,
                    value: value,
                    params: to.params,
                    query: to.query,
                    meta: meta,
                    group: router.$avueRouter.group || []
                });
            }
            next()
            // }
        }
    } else {
        //判断是否需要认证，没有登录访问去登录页
        if (meta.isAuth === false) {
            next()
        } else {
            next('/login')
        }
    }
})

router.afterEach(to => {
    NProgress.done();
    let title = router.$avueRouter.generateTitle(to)
    router.$avueRouter.setTitle(title);
    // store.commit('SET_IS_SEARCH', false)
});